/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.minierp.bean.EstablishmentCommunicateBean;
import com.minierp.bean.OrderParameterCarrier;
import com.minierp.bean.QueryParameterCarrier;
import com.minierp.bean.QueryResult;
import com.minierp.bean.UserBean;
import com.minierp.bean.UserStatus;
import com.minierp.dao.DepartmentDao;
import com.minierp.dao.EstablishmentCommunicateDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.QueryDao;
import com.minierp.dao.StafferDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncInterceptorHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.EstCommReceiver;
import com.minierp.model.Establishment;
import com.minierp.model.EstablishmentCommunicate;
import com.minierp.model.Staffer;
import com.minierp.view.EstablishmentCommunicateCalendarExcelView;
import com.minierp.view.EstablishmentCommunicateSearchResultExcelView;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class EstablishmentCommunicateMACtr extends MultiActionController {
	private EstablishmentCommunicateDao establishmentCommunicateDao;
	private DepartmentDao departmentDao;
	private StafferDao stafferDao;
	private QueryDao queryDao;
	private IdGenerator idGenerator;
	private String listView;
	private String dateCalendarView;
	private String weekCalendarView;
	private String monthCalendarView;
	private String receiverListView;
	private String searchResultView;
	private String compFormView;
	private String underListView;

	public DepartmentDao getDepartmentDao() {
		return this.departmentDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public String getUnderListView() {
		return this.underListView;
	}

	public void setUnderListView(String underListView) {
		this.underListView = underListView;
	}

	public String getCompFormView() {
		return this.compFormView;
	}

	public void setCompFormView(String compFormView) {
		this.compFormView = compFormView;
	}

	public String getDateCalendarView() {
		return this.dateCalendarView;
	}

	public void setDateCalendarView(String dateCalendarView) {
		this.dateCalendarView = dateCalendarView;
	}

	public String getMonthCalendarView() {
		return this.monthCalendarView;
	}

	public void setMonthCalendarView(String monthCalendarView) {
		this.monthCalendarView = monthCalendarView;
	}

	public String getWeekCalendarView() {
		return this.weekCalendarView;
	}

	public void setWeekCalendarView(String weekCalendarView) {
		this.weekCalendarView = weekCalendarView;
	}

	public ModelAndView handleList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listView);
		String objName = "com.minierp.model.EstablishmentCommunicate";
		String objShortName = "b";
		HashMap shortNameMap = new HashMap();
		String[] retObjs = new String[]{objShortName};
		Vector qpVe = new Vector();
		shortNameMap.put(objName, objShortName);
		String clientId = request.getParameter("client_id");
		QueryParameterCarrier qp = new QueryParameterCarrier();
		qp.objectName = objName;
		qp.propertyName = "estId";
		qp.value = " b.estId=" + clientId;
		qp.dataType = 6;
		qp.logicType = 10;
		qpVe.add(qp);
		mv.addObject("clientId", clientId);
		QueryParameterCarrier[] qParas = new QueryParameterCarrier[qpVe.size()];

		for (int p1 = 0; p1 < qpVe.size(); ++p1) {
			qParas[p1] = (QueryParameterCarrier) qpVe.get(p1);
		}

		OrderParameterCarrier arg15 = new OrderParameterCarrier();
		arg15.objectName = objName;
		arg15.propertyName = "communicateTime";
		arg15.orderTarget = "desc";
		OrderParameterCarrier[] oParas = new OrderParameterCarrier[]{arg15};
		int currentPage = EncCommonHelper.getCurrentPageIndex(request);
		QueryResult qr = this.queryDao.doQuery(currentPage,
				EncCommonHelper.getPageSize(request), qParas, oParas,
				(String[]) null, shortNameMap, retObjs, (String[]) null);
		mv.addObject("qr", qr);
		mv.addObject(
				"est",
				this.establishmentCommunicateDao.get(Establishment.class,
						Long.parseLong(clientId)));
		return mv;
	}

	public ModelAndView handleReceiverList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.receiverListView);
		String commId = request.getParameter("comm_id");
		String tmpCode = request.getParameter("tmp_code");
		List list;
		if (commId != null && !commId.equals("")) {
			list = this.establishmentCommunicateDao.getReceiverList(Long
					.parseLong(commId));
			mv.addObject("commId", commId);
		} else {
			list = this.establishmentCommunicateDao.getReceiverList(tmpCode);
			mv.addObject("tmpCode", tmpCode);
		}

		mv.addObject("list", list);
		return mv;
	}

	public ModelAndView handleReceiverAdd(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String commId = request.getParameter("comm_id");
		String tmpCode = request.getParameter("tmp_code");
		String idStr = request.getParameter("id_str");
		String[] arr = idStr.split(";");

		for (int i = 0; i < arr.length; ++i) {
			EstCommReceiver existRec = null;
			long receiverId = Long.parseLong(arr[i]);
			if (commId != null && !commId.equals("")) {
				existRec = this.establishmentCommunicateDao.getReceiver(
						Long.parseLong(commId), receiverId);
			} else {
				existRec = this.establishmentCommunicateDao.getReceiver(
						tmpCode, receiverId);
			}

			if (existRec == null) {
				EstCommReceiver r = new EstCommReceiver();
				if (commId != null && !commId.equals("")) {
					r.setCommId(Long.parseLong(commId));
				} else {
					r.setTmpCode(tmpCode);
				}

				r.setId(this.idGenerator.getId(EstCommReceiver.class.getName()));
				r.setReceiverId(receiverId);
				this.establishmentCommunicateDao.save(r);
				EncCommonHelper.setAlertMessage(request, "增加成功！");
			}
		}

		return new ModelAndView(new RedirectView(
				"/base/EstCommReceiverList.ihtm?comm_id=" + commId
						+ "&tmp_code=" + tmpCode));
	}

	public ModelAndView handleReceiverDel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String commId = request.getParameter("comm_id");
		String tmpCode = request.getParameter("tmp_code");
		String id = request.getParameter("id");
		this.establishmentCommunicateDao.deleteReceiver(Long.parseLong(id));
		EncCommonHelper.setAlertMessage(request, "删除成功！");
		return new ModelAndView(new RedirectView(
				"/base/EstCommReceiverList.ihtm?comm_id=" + commId
						+ "&tmp_code=" + tmpCode));
	}

	public ModelAndView handleComplete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String mark = request.getParameter("mark");
		this.establishmentCommunicateDao.complete(Long.parseLong(id), mark);
		ModelAndView mv = new ModelAndView("redirect:/ModalDialogSuccess.ihtm");
		mv.addObject("returnValue", id);
		return mv;
	}

	public ModelAndView handleDelete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		EstablishmentCommunicate ec = (EstablishmentCommunicate) this.establishmentCommunicateDao
				.get(EstablishmentCommunicate.class, Long.parseLong(id));
		boolean canEdit = false;
		if (ub.getUser().getUserType() == 1) {
			canEdit = true;
		} else if (ec != null) {
			if (ec.getCommType().equals("LOG")) {
				SimpleDateFormat returnUrl = new SimpleDateFormat("yyyy-MM-dd");
				if (ub.getUser().getStafferId() == ec.getCreaterId()
						&& returnUrl.format(new Date()).equals(
								returnUrl.format(ec.getCreateTime()))) {
					canEdit = true;
				} else {
					canEdit = false;
				}
			} else if (ub.getUser().getStafferId() == ec.getCreaterId()
					&& ec.getCompleted().equals("N")) {
				canEdit = true;
			} else {
				canEdit = false;
			}
		} else {
			canEdit = true;
		}

		if (!canEdit) {
			throw new Exception("Permission deny!");
		} else {
			if (id != null && !id.equals("")) {
				this.establishmentCommunicateDao.delete(Long.parseLong(id));
			}

			String returnUrl1 = request.getParameter("from_url");
			ModelAndView mv;
			if (returnUrl1 != null && !returnUrl1.equals("")) {
				mv = new ModelAndView("redirect:" + returnUrl1);
				EncCommonHelper.setAlertMessage(request, "删除成功！");
			} else {
				mv = new ModelAndView("redirect:/ModalDialogSuccess.ihtm");
				mv.addObject("returnValue", id);
			}

			return mv;
		}
	}

	public ModelAndView handleCalendar(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat yearSf = new SimpleDateFormat("yyyy");
		SimpleDateFormat monthSf = new SimpleDateFormat("MM");
		String type = request.getParameter("type");
		if (type == null) {
			type = "";
		}

		String action = request.getParameter("action");
		String currentDate = request.getParameter("current_date");
		Date startDate = this.getCalendarStartDate(type, action, currentDate);
		Date endDate = this.getCalendarEndDate(type, startDate);
		String startDateStr = sf.format(startDate);
		String endDateStr = sf.format(endDate);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		String stafferId = request.getParameter("staffer_id");
		if (stafferId == null || stafferId.equals("")) {
			stafferId = String.valueOf(ub.getStaffer().getId());
		}

		List list = this.establishmentCommunicateDao.getList(startDate,
				sf.parse(EncCommonHelper.getNextDateStr(endDateStr,
						"yyyy-MM-dd")), Long.parseLong(stafferId));
		ArrayList arr = new ArrayList();
		ModelAndView mv;
		ArrayList itemArr;
		Object[] objs;
		EstablishmentCommunicate ec;
		int v;
		if (type.equals("WEEK")) {
			mv = new ModelAndView(this.weekCalendarView);
			Date contentType;
			if (currentDate != null && !currentDate.equals("")) {
				if (action != null && !action.equals("")) {
					contentType = startDate;
				} else {
					contentType = sf.parse(currentDate);
				}
			} else {
				contentType = new Date();
			}

			mv.addObject("year", yearSf.format(contentType));
			mv.addObject("month", monthSf.format(contentType));
			if (startDate.after(new Date())) {
				mv.addObject("lastYear", yearSf.format(startDate));
			} else {
				mv.addObject("lastYear", yearSf.format(new Date()));
			}

			String map = sf.format(startDate);
			Calendar staf = Calendar.getInstance();

			for (v = 0; v < 7; ++v) {
				staf.setTime(sf.parse(map));
				staf.add(5, 1);
				Date bean = staf.getTime();
				itemArr = new ArrayList();

				for (int est = 0; est < list.size(); ++est) {
					ec = (EstablishmentCommunicate) list.get(est);
					if (map.equals(sf.format(ec.getCommunicateTime()))) {
						EstablishmentCommunicateBean rowNum = new EstablishmentCommunicateBean();
						rowNum.setCommunicate(ec);
						if (ec.getEstId() != 0L) {
							Establishment i = (Establishment) this.stafferDao
									.get(Establishment.class, ec.getEstId());
							if (i != null) {
								rowNum.setEstName(i.getEstName());
							}
						}

						itemArr.add(rowNum);
					}
				}

				objs = new Object[]{this.getWeekName(sf.parse(map)), map,
						itemArr};
				arr.add(objs);
				map = sf.format(bean);
			}
		} else {
			int arg35;
			if (type.equals("MONTH")) {
				mv = new ModelAndView(this.monthCalendarView);
				mv.addObject("year", yearSf.format(startDate));
				mv.addObject("month", monthSf.format(startDate));
				if (startDate.after(new Date())) {
					mv.addObject("lastYear", yearSf.format(startDate));
				} else {
					mv.addObject("lastYear", yearSf.format(new Date()));
				}

				Calendar arg33 = Calendar.getInstance();
				arg33.setTime(startDate);
				arg35 = arg33.get(7) - 1;
				long arg37 = 86400000L;
				int arg41 = (int) ((endDate.getTime() - startDate.getTime())
						/ arg37 + 1L);
				arg41 += arg35;
				byte arg44 = 7;
				int arg46 = arg41 / arg44;
				if (arg46 * arg44 < arg41) {
					++arg46;
				}

				arg33.add(5, -arg35);

				for (int arg47 = 0; arg47 < arg46; ++arg47) {
					for (int j = 0; j < arg44; ++j) {
						int index = arg47 * arg44 + j;
						objs = new Object[2];
						if (!arg33.getTime().before(startDate)
								&& !arg33.getTime().after(endDate)) {
							itemArr = new ArrayList();

							for (int m = 0; m < list.size(); ++m) {
								ec = (EstablishmentCommunicate) list.get(m);
								if (sf.format(arg33.getTime()).equals(
										sf.format(ec.getCommunicateTime()))) {
									EstablishmentCommunicateBean bean1 = new EstablishmentCommunicateBean();
									bean1.setCommunicate(ec);
									if (ec.getEstId() != 0L) {
										Establishment est1 = (Establishment) this.stafferDao
												.get(Establishment.class,
														ec.getEstId());
										if (est1 != null) {
											bean1.setEstName(est1.getEstName());
										}
									}

									itemArr.add(bean1);
								}
							}

							objs[0] = String.valueOf(index + 1 - arg35);
							objs[1] = itemArr;
						}

						arr.add(objs);
						arg33.add(5, 1);
					}
				}

				mv.addObject("rowNum", String.valueOf(arg46));
			} else {
				mv = new ModelAndView(this.dateCalendarView);
				mv.addObject("title",
						startDateStr + " " + this.getWeekName(startDate));
				SimpleDateFormat arg34 = new SimpleDateFormat("HH");

				for (arg35 = 0; arg35 < 24; ++arg35) {
					int arg38 = arg35 + 8;
					if (arg38 >= 24) {
						arg38 -= 24;
					}

					itemArr = new ArrayList();

					for (v = 0; v < list.size(); ++v) {
						ec = (EstablishmentCommunicate) list.get(v);
						if (Integer.parseInt(arg34.format(ec
								.getCommunicateTime())) == arg38) {
							EstablishmentCommunicateBean arg43 = new EstablishmentCommunicateBean();
							arg43.setCommunicate(ec);
							if (ec.getEstId() != 0L) {
								Establishment arg45 = (Establishment) this.stafferDao
										.get(Establishment.class, ec.getEstId());
								if (arg45 != null) {
									arg43.setEstName(arg45.getEstName());
								}
							}

							itemArr.add(arg43);
						}
					}

					objs = new Object[2];
					if (arg38 < 10) {
						objs[0] = "0" + String.valueOf(arg38) + ":00";
					} else {
						objs[0] = String.valueOf(arg38) + ":00";
					}

					objs[1] = itemArr;
					arr.add(objs);
				}
			}
		}

		String arg36 = request.getParameter("content_type");
		if (arg36 != null && arg36.equals("EXCEL")) {
			HashMap arg39 = new HashMap();
			arg39.put("list", arr);
			arg39.put("type", type);
			Staffer arg40 = (Staffer) this.stafferDao.get(Staffer.class,
					Long.parseLong(stafferId));
			if (arg40 != null) {
				arg39.put("stafName", arg40.getStafferName());
			}

			EstablishmentCommunicateCalendarExcelView arg42 = new EstablishmentCommunicateCalendarExcelView();
			return new ModelAndView(arg42, arg39);
		} else {
			mv.addObject("list", arr);
			mv.addObject("startDate", startDateStr);
			mv.addObject("endDate", endDateStr);
			mv.addObject("stafferId", stafferId);
			mv.addObject("ub", ub);
			if (!EncCommonHelper.checkPurePrivilege(request, "COMM_SELF")) {
				mv.addObject("stafferList", this.stafferDao.getAllChildren(ub
						.getStaffer().getId(), true));
			}

			return mv;
		}
	}

	private String getWeekName(Date d) {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		int dayOfWeek = c.get(7);
		String name = "";
		switch (dayOfWeek) {
			case 1 :
				name = "日";
				break;
			case 2 :
				name = "一";
				break;
			case 3 :
				name = "二";
				break;
			case 4 :
				name = "三";
				break;
			case 5 :
				name = "四";
				break;
			case 6 :
				name = "五";
				break;
			case 7 :
				name = "六";
		}

		return "星期" + name;
	}

	private Date getCalendarStartDate(String type, String action,
			String currentDateStr) throws Exception {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		if (currentDateStr == null || currentDateStr.equals("")) {
			currentDateStr = sf.format(new Date());
		}

		Date currentDate = sf.parse(currentDateStr);
		Calendar c = Calendar.getInstance();
		Date startDate;
		if (type.equals("WEEK")) {
			c.setTime(currentDate);
			int shortSf = c.get(7) - 1;
			c.add(5, -shortSf);
			startDate = c.getTime();
		} else if (type.equals("MONTH")) {
			SimpleDateFormat shortSf1 = new SimpleDateFormat("yyyy-MM");
			String startDateStr = shortSf1.format(currentDate) + "-01";
			startDate = sf.parse(startDateStr);
		} else {
			startDate = currentDate;
		}

		if (action != null) {
			c.setTime(startDate);
			if (action.equals("PREVIEW")) {
				if (type.equals("WEEK")) {
					c.add(5, -7);
					startDate = c.getTime();
				} else if (type.equals("MONTH")) {
					c.add(2, -1);
					startDate = c.getTime();
				} else {
					c.add(5, -1);
					startDate = c.getTime();
				}
			} else if (action.equals("NEXT")) {
				if (type.equals("WEEK")) {
					c.add(5, 7);
					startDate = c.getTime();
				} else if (type.equals("MONTH")) {
					c.add(2, 1);
					startDate = c.getTime();
				} else {
					c.add(5, 1);
					startDate = c.getTime();
				}
			}
		}

		return startDate;
	}

	private Date getCalendarEndDate(String type, Date startDate)
			throws Exception {
		Calendar c = Calendar.getInstance();
		c.setTime(startDate);
		if (type.equals("WEEK")) {
			c.add(5, 6);
		} else if (type.equals("MONTH")) {
			c.add(2, 1);
			c.add(5, -1);
		}

		return c.getTime();
	}

	public ModelAndView handleSearchResult(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.searchResultView);
		String searchType = request.getParameter("search_type");
		String type = request.getParameter("type");
		String createrName = request.getParameter("creater_name");
		String createrId = request.getParameter("creater_id");
		String completed = request.getParameter("completed");
		String startDateStr = request.getParameter("start_date");
		String endDateStr = request.getParameter("end_date");
		String fromType = request.getParameter("from_type");
		Date startDate = null;
		Date endDate = null;
		String typeTmp = "A";
		String completedTmp = "A";
		String fromTypeTmp = "A";
		long createrIdTmp = 0L;
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		long stafferId = ub.getStaffer().getId();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat shortSf = new SimpleDateFormat("yyyy-MM");
		String contentType;
		if (searchType != null && !searchType.equals("")) {
			if (searchType.equals("2")) {
				startDate = sf.parse(startDateStr);
				endDate = sf.parse(EncCommonHelper.getNextDateStr(endDateStr,
						"yyyy-MM-dd"));
			} else {
				String total1 = request.getParameter("cb_type");
				contentType = request.getParameter("cb_creater");
				String perPageNum = request.getParameter("cb_complete");
				String pageno = request.getParameter("cb_comm_date");
				String startIndex = request.getParameter("cb_from");
				if (total1 != null && total1.equals("1")) {
					typeTmp = type;
					mv.addObject("cbType", total1);
				}

				if (contentType != null && contentType.equals("1")) {
					createrIdTmp = Long.parseLong(createrId);
					mv.addObject("cbCreater", contentType);
				}

				if (perPageNum != null && perPageNum.equals("1")) {
					completedTmp = completed;
					mv.addObject("cbComplete", perPageNum);
				}

				if (pageno != null && pageno.equals("1")) {
					startDate = sf.parse(startDateStr);
					endDate = sf.parse(EncCommonHelper.getNextDateStr(
							endDateStr, "yyyy-MM-dd"));
					mv.addObject("cbCommDate", pageno);
				}

				if (startIndex != null && startIndex.equals("1")) {
					fromTypeTmp = fromType;
					mv.addObject("cbFrom", startIndex);
				}
			}
		} else {
			searchType = "2";
			Date total = new Date();
			startDateStr = shortSf.format(total) + "-01";
			endDateStr = sf.format(total);
			startDate = sf.parse(startDateStr);
			endDate = sf.parse(EncCommonHelper.getNextDateStr(endDateStr,
					"yyyy-MM-dd"));
		}

		int total2 = this.establishmentCommunicateDao.getListCount(startDate,
				endDate, typeTmp, completedTmp, stafferId, createrIdTmp,
				fromTypeTmp);
		contentType = request.getParameter("content_type");
		if (contentType != null && contentType.equals("EXCEL")) {
			HashMap perPageNum2 = new HashMap();
			List pageno2 = this.establishmentCommunicateDao.getList(startDate,
					endDate, typeTmp, completedTmp, stafferId, createrIdTmp,
					fromTypeTmp, 0, total2);
			perPageNum2.put("list", pageno2);
			perPageNum2.put("stafName", ub.getStaffer().getStafferName());
			EstablishmentCommunicateSearchResultExcelView startIndex2 = new EstablishmentCommunicateSearchResultExcelView();
			return new ModelAndView(startIndex2, perPageNum2);
		} else {
			int perPageNum1 = EncCommonHelper.getPageSize(request);
			int pageno1 = EncCommonHelper.getCurrentPageIndex(request, total2,
					perPageNum1);
			int startIndex1 = (pageno1 - 1) * perPageNum1;
			List list = this.establishmentCommunicateDao.getList(startDate,
					endDate, typeTmp, completedTmp, stafferId, createrIdTmp,
					fromTypeTmp, startIndex1, perPageNum1);
			mv.addObject("qr", EncCommonHelper.parseQueryResult(list, total2,
					perPageNum1, pageno1));
			mv.addObject("createrName", createrName);
			mv.addObject("createrId", createrId);
			mv.addObject("completed", completed);
			mv.addObject("startDate", startDateStr);
			mv.addObject("endDate", endDateStr);
			mv.addObject("searchType", searchType);
			mv.addObject("type", type);
			mv.addObject("fromType", fromType);
			return mv;
		}
	}

	public ModelAndView handleCompForm(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.compFormView);
		mv.addObject("id", request.getParameter("id"));
		return mv;
	}

	public ModelAndView handleUnderList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.underListView);
		String searchType = request.getParameter("search_type");
		String dep = request.getParameter("dep");
		String staName = request.getParameter("sta_name");
		String comp = request.getParameter("comp");
		String startDateStr = request.getParameter("start_date");
		String endDateStr = request.getParameter("end_date");
		Date startDate = null;
		Date endDate = null;
		String compTmp = "A";
		String staNameTmp = "";
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		long depId = 0L;
		long stafferId = ub.getStaffer().getId();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat shortSf = new SimpleDateFormat("yyyy-MM");
		String contentType;
		if (searchType != null && !searchType.equals("")) {
			if (searchType.equals("2")) {
				startDate = sf.parse(startDateStr);
				endDate = sf.parse(EncCommonHelper.getNextDateStr(endDateStr,
						"yyyy-MM-dd"));
			} else {
				String total1 = request.getParameter("cb_dep");
				contentType = request.getParameter("cb_sta");
				String perPageNum = request.getParameter("cb_comp");
				String pageno = request.getParameter("cb_comm_date");
				if (total1 != null && total1.equals("1")) {
					depId = Long.parseLong(dep);
					mv.addObject("cbDep", total1);
				}

				if (perPageNum != null && perPageNum.equals("1")) {
					compTmp = comp;
					mv.addObject("cbComp", perPageNum);
				}

				if (pageno != null && pageno.equals("1")) {
					startDate = sf.parse(startDateStr);
					endDate = sf.parse(EncCommonHelper.getNextDateStr(
							endDateStr, "yyyy-MM-dd"));
					mv.addObject("cbCommDate", pageno);
				}

				if (contentType != null && contentType.equals("1")) {
					staNameTmp = staName;
					mv.addObject("cbSta", contentType);
				}
			}
		} else {
			searchType = "2";
			Date total = new Date();
			startDateStr = shortSf.format(total) + "-01";
			endDateStr = sf.format(total);
			startDate = sf.parse(startDateStr);
			endDate = sf.parse(EncCommonHelper.getNextDateStr(endDateStr,
					"yyyy-MM-dd"));
		}

		int total2 = this.establishmentCommunicateDao.getUnderListCount(
				startDate, endDate, depId, staNameTmp, compTmp, stafferId);
		contentType = request.getParameter("content_type");
		if (contentType != null && contentType.equals("EXCEL")) {
			HashMap perPageNum2 = new HashMap();
			List pageno2 = this.establishmentCommunicateDao.getUnderList(
					startDate, endDate, depId, staNameTmp, compTmp, stafferId,
					0, total2);
			perPageNum2.put("list", pageno2);
			perPageNum2.put("listType", "UNDER");
			EstablishmentCommunicateSearchResultExcelView startIndex1 = new EstablishmentCommunicateSearchResultExcelView();
			return new ModelAndView(startIndex1, perPageNum2);
		} else {
			int perPageNum1 = EncCommonHelper.getPageSize(request);
			int pageno1 = EncCommonHelper.getCurrentPageIndex(request, total2,
					perPageNum1);
			int startIndex = (pageno1 - 1) * perPageNum1;
			List list = this.establishmentCommunicateDao.getUnderList(
					startDate, endDate, depId, staNameTmp, compTmp, stafferId,
					startIndex, perPageNum1);
			mv.addObject("qr", EncCommonHelper.parseQueryResult(list, total2,
					perPageNum1, pageno1));
			mv.addObject("staName", staName);
			mv.addObject("dep", dep);
			mv.addObject("comp", comp);
			mv.addObject("startDate", startDateStr);
			mv.addObject("endDate", endDateStr);
			mv.addObject("searchType", searchType);
			mv.addObject("depList", this.departmentDao.getList(true));
			return mv;
		}
	}

	public ModelAndView handleAppList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap map = new HashMap();
		UserStatus s = EncInterceptorHelper.checkAppUserSignon(request);
		UserBean ub = s.getUserBean();
		String json;
		if (ub == null) {
			map.put("status", "NO_LOGIN");
			map.put("errMsg", "");
		} else {
			try {
				String mapper = request.getParameter("est_id");
				json = request.getParameter("show_prev");
				int out = this.establishmentCommunicateDao
						.getListForEstCount(Long.parseLong(mapper));
				int perPageNum = EncCommonHelper.getPageSize(request);
				int pageno = EncCommonHelper.getCurrentPageIndex(request, out,
						perPageNum);
				if (out > pageno * perPageNum) {
					map.put("more", "Y");
				} else {
					map.put("more", "N");
				}

				List list;
				if (json != null && json.equals("Y")) {
					list = this.establishmentCommunicateDao.getListForEst(
							Long.parseLong(mapper), 0, pageno * perPageNum);
				} else {
					list = this.establishmentCommunicateDao.getListForEst(
							Long.parseLong(mapper), (pageno - 1) * perPageNum,
							perPageNum);
				}

				Calendar c = Calendar.getInstance();
				Date now = new Date();
				c.setTime(now);
				long currMill = c.getTimeInMillis();
				long oneMinu = 60000L;
				long oneHour = 3600000L;
				long oneDay = 86400000L;
				ArrayList items = new ArrayList();

				for (int i = 0; i < list.size(); ++i) {
					HashMap item = new HashMap();
					Object[] objs = (Object[]) list.get(i);
					String creaTime = "";
					c.setTime((Date) objs[2]);
					long diffMill = currMill - c.getTimeInMillis();
					if (diffMill < oneMinu) {
						creaTime = "现在";
					} else {
						long num;
						if (diffMill > oneMinu && diffMill < oneHour) {
							num = diffMill / oneMinu;
							creaTime = num + "分钟前";
						} else if (diffMill > oneHour && diffMill < oneDay) {
							num = diffMill / oneHour;
							creaTime = num + "小时前";
						} else {
							num = diffMill / oneDay;
							creaTime = num + "天前";
						}
					}

					item.put("id", objs[0]);
					item.put("creaName", objs[1]);
					item.put("creaTime", creaTime);
					item.put("cont", objs[3]);
					item.put("headImg", PageHelper.parseNull((String) objs[4]));
					items.add(item);
				}

				map.put("items", items);
				map.put("status", "OK");
				map.put("errMsg", "");
			} catch (Exception arg30) {
				map.put("status", "ERROR");
				map.put("errMsg", arg30.toString());
			}
		}

		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ObjectMapper arg31 = new ObjectMapper();
		json = arg31.writeValueAsString(map);
		ServletOutputStream arg32 = response.getOutputStream();
		arg32.write(json.getBytes("utf-8"));
		arg32.flush();
		arg32.close();
		return null;
	}

	public ModelAndView handleAppNew(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap map = new HashMap();
		UserStatus s = EncInterceptorHelper.checkAppUserSignon(request);
		UserBean ub = s.getUserBean();
		String json;
		if (ub == null) {
			map.put("status", "NO_LOGIN");
			map.put("errMsg", "");
		} else {
			try {
				String mapper = request.getParameter("est_id");
				json = request.getParameter("cont");
				Date out = new Date();
				String title = json;
				if (json.length() > 10) {
					title = json.substring(0, 10);
				}

				long id = this.idGenerator.getId(EstablishmentCommunicate.class
						.getName());
				EstablishmentCommunicate c = new EstablishmentCommunicate();
				c.setId(id);
				c.setCommunicateTime(out);
				c.setCommType("LOG");
				c.setContent(json);
				c.setCreaterId(ub.getStaffer().getId());
				c.setCreaterName(ub.getStaffer().getStafferName());
				c.setCreateTime(out);
				c.setEstId(Long.parseLong(mapper));
				c.setTitle(title);
				this.establishmentCommunicateDao.save(c);
				Establishment e = (Establishment) this.establishmentCommunicateDao
						.get(Establishment.class, c.getEstId());
				if (e != null) {
					e.setCommTime(out);
					this.establishmentCommunicateDao.update(e);
				}

				map.put("id", Long.valueOf(id));
				map.put("cont", json);
				map.put("creaName", c.getCreaterName());
				map.put("headImg",
						PageHelper.parseNull(ub.getStaffer().getHeadImage()));
				map.put("creaTime", "现在");
				map.put("status", "OK");
				map.put("errMsg", "");
			} catch (Exception arg13) {
				map.put("status", "ERROR");
				map.put("errMsg", arg13.getMessage());
				arg13.printStackTrace();
			}
		}

		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ObjectMapper mapper1 = new ObjectMapper();
		json = mapper1.writeValueAsString(map);
		ServletOutputStream out1 = response.getOutputStream();
		out1.write(json.getBytes("utf-8"));
		out1.flush();
		out1.close();
		return null;
	}

	public String getListView() {
		return this.listView;
	}

	public void setListView(String listView) {
		this.listView = listView;
	}

	public EstablishmentCommunicateDao getEstablishmentCommunicateDao() {
		return this.establishmentCommunicateDao;
	}

	public void setEstablishmentCommunicateDao(
			EstablishmentCommunicateDao establishmentCommunicateDao) {
		this.establishmentCommunicateDao = establishmentCommunicateDao;
	}

	public QueryDao getQueryDao() {
		return this.queryDao;
	}

	public void setQueryDao(QueryDao queryDao) {
		this.queryDao = queryDao;
	}

	public StafferDao getStafferDao() {
		return this.stafferDao;
	}

	public void setStafferDao(StafferDao stafferDao) {
		this.stafferDao = stafferDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public String getReceiverListView() {
		return this.receiverListView;
	}

	public void setReceiverListView(String receiverListView) {
		this.receiverListView = receiverListView;
	}

	public String getSearchResultView() {
		return this.searchResultView;
	}

	public void setSearchResultView(String searchResultView) {
		this.searchResultView = searchResultView;
	}
}